package com.streambolics.worseminton.servlet;

import javax.servlet.http.HttpServletRequest;

import com.streambolics.appengine.UserAwareServlet;
import com.streambolics.data.Person;
import com.streambolics.servlet.RequestBeanSetter;
import com.streambolics.std.HtmlBuilder;
import com.streambolics.worseminton.data.Invitation;

public class InvitationHandler extends UserAwareServlet<Invitation>
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    protected boolean currentUserIsOwner (Invitation aInvitation)
    {
        return aInvitation.isAddressedTo (getCurrentUserId ());
    }

    @Override
    protected void formatBodyAsSimpleDesktopHtml (HtmlBuilder aBuilder, Invitation aResponseObject) throws Exception
    {
        aBuilder.openDiv ("body");
        aBuilder.openElement ("<h1>");
        aBuilder.addText ("You are invited to " + aResponseObject.getEvent ().getDescription ());
        aBuilder.closeElement ("</h1>");
        formatOwnerError (aBuilder, aResponseObject);

        if (hasWritePermission (aResponseObject))
        {
            aBuilder.openForm ("/go/Invitation");
            aBuilder.addHidden ("ID", aResponseObject.getIdAsString ());
            aBuilder.addAnchor ("/go/InvitationAccept?ID=" + aResponseObject.getIdAsString (), "Accept the invitation");
            aBuilder.addBreak ();
            aBuilder.addAnchor ("/go/InvitationDecline?ID=" + aResponseObject.getIdAsString (), "Decline the invitation");
            aBuilder.addRule ();
            aBuilder.addAnchor ("/go/InvitationUnsubscribe?ID=" + aResponseObject.getIdAsString (), "Click here to stop receiving change nofications about this particular event");
            aBuilder.addBreak ();
            aBuilder.closeForm ();

            aBuilder.openForm ("/go/LocationInterestUpdate");
            aBuilder.addHidden ("ID", aResponseObject.getLocation ().getIdAsString ());
            aBuilder.addParagraph ("Use the following options to change the way you receceive notifications in the future.");

            aBuilder.addCheckBox ("UNSUBSCRIBE", false);
            aBuilder.addText ("Unsubscribe. You will no longer receive e-mail notifications about events in " + aResponseObject.getLocation ().getName ());

            aBuilder.addBreak ();
            aBuilder.addCheckBox ("FORWARD", false);
            aBuilder.addTextField ("NEWEMAIL", "Use other e-mail:");

            aBuilder.addBreak ();
            aBuilder.addCheckBox ("RENAME", false);
            aBuilder.addTextField ("NEWNAME", "Change your display name to:");

            aBuilder.addSubmitButton ("OK", "Update your preferences");
            aBuilder.closeForm ();
        }
        aBuilder.closeDiv ();
    }

    @Override
    protected void formatBottomAsSimpleDesktopHtml (HtmlBuilder aBuilder, Invitation aResponseObject) throws Exception
    {
        aBuilder.openDiv ("bottom");
        aBuilder.addAnchor ("/go/Event?ID=" + aResponseObject.getEvent ().getIdAsString (), "See event details");
        // TODO : This should go in a "NonHomeServlet"
        aBuilder.addAnchor ("/go/HomeInfo", "Back to home screen");
        super.formatBottomAsSimpleDesktopHtml (aBuilder, aResponseObject);
        aBuilder.closeDiv ();
    }

    protected void formatOwnerError (HtmlBuilder aBuilder, Invitation aInvitation) throws Exception
    {
        if (!currentUserIsOwner (aInvitation))
        {
            aBuilder.openForm ("/go/HomeInfo");
            aBuilder.addParagraph ("Please pay attention to the fact that you are not currently logged as the guest of this invitation. If the real guest has protected his account with a password, you will not be able to accept or decline the invitation. Use the Log-In button at the top right of the screen to log-in as the appropriate user before proceeding.");
            Person p = getPerson ();
            if (p != null)
            {
                aBuilder.addParagraph ("You are currenlty logged with e-mail address " + p.getEmail ());
            }
            aBuilder.addParagraph ("The invitation is for " + aInvitation.getGuest ().getEmail ());
        }

    }

    @Override
    protected Invitation generateResponseObject (HttpServletRequest aRequest) throws Exception
    {
        return getReferencedInvitation ();
    }

    protected Invitation getReferencedInvitation () throws Exception
    {
        return Invitation.load (getDatabase (), requireReferenceId ());
    }

    @Override
    protected boolean hasWritePermission (Invitation aInvitation)
    {
        return aInvitation.mayBeModifiedBy (getCurrentUserId ());
    }

    protected void updateComments (Invitation aInvitation) throws Exception
    {
        RequestBeanSetter s = new RequestBeanSetter (getRequest (), aInvitation);
        s.set ("Comment", "COMMENT");
        s.set ("PrivateComment", "PRIVATECOMMENT");
    }

}
